<?php
$boxid = $_GET['valid'];
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>BigUpload</title>
<script type="text/javascript" src="../lzsswfupload/jquery.js"></script>
<script language="javascript" src="../lzsswfupload/swfupload.js"></script>
<script type="text/javascript">
jQuery.fn.outerHTML = function(s) {
	return (s) ? this.before(s).remove() : jQuery("p").append(this.eq(0).clone()).html();
}
function Bigupload(uploadselectid,uploadstartid,swfuploadbox,swfuploadlistbox,swfuploadimageslistid,swfuploadimageshow){
	this.uploadselectid = uploadselectid;  // 选择按钮id
	this.uploadstartid = uploadstartid; // 开始上传按钮的id
	this.swfuploadbox = swfuploadbox; // 上传队列的boxid
	this.swfuploadlistbox = swfuploadlistbox; // 上传队列的文件列id
	this.swfuploadimageslistid = swfuploadimageslistid; // 表单存储图片信息的id 
	this.swfuploadimageshow = swfuploadimageshow; // 图片显示id
	var swfu;
	var filevalues = new Array();
		// 初始化
		this.start=function(){
			$('#'+this.swfuploadbox).html('<ul class="swfuploadfilelist" id="'+this.swfuploadlistbox+'"></ul>');
			swfu=new SWFUpload({
				upload_url:'../lzsswfupload/swfuploadbigfile.php',
				flash_url:'../lzsswfupload/swfupload.swf',
				/*
				file_size_limit:'1500MB',
				file_types:"*.*",
				*/
				file_size_limit:'1024MB',
				file_upload_limit:0,
				file_queue_limit:1,
				file_types:"<?php echo isset($_GET['ext'])?$_GET['ext']:'*.*';?>",
				/* 按钮的设置 */
				button_image_url: "../lzsswfupload/newbigswfuplogbuttom.jpg", // Relative to the Flash file
				button_placeholder_id : this.uploadselectid,
				button_width: 160,
				button_height: 29,
				button_text_top_padding: 0,
				button_text_left_padding: 0,
				button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
				button_cursor: SWFUpload.CURSOR.HAND,
				button_action: SWFUpload.BUTTON_ACTION.SELECT_FILE,
				
				/* 选择完图片后执行。 */
				file_dialog_complete_handler:fileDialogComplete,
				/* 上传成功后执行。 */
				upload_success_handler:uploadSuccess,
				/* 主要是进度条 */
				upload_progress_handler:uploadProgress,
				/* 循环队列里的图片上传 */
				upload_complete_handler: uploadComplete,
				/* 主要是把图片加入队列 */
				file_queued_handler:fileQueued,
				
				debug: false
			});
			swfu.swfuploadbox = this.swfuploadbox
			swfu.filelistbox = this.swfuploadlistbox;
			swfu.swfuploadimageslistid = this.swfuploadimageslistid;
			swfu.swfuploadimageshow = this.swfuploadimageshow;
			// 执行上传
			$('#'+this.uploadstartid).click(function(){
				if(swfu.getStats().files_queued > 0){
					swfu.startUpload();
				}else{
					alert('请先选择要上传的文件');
				}
			});
		}
		/*
			这个方法作用： 就是选择完图片后关闭时执行。
			参数：numFilesQueued 就是先择图片的数量
		*/
		function fileDialogComplete(numFilesSelected, numFilesQueued){
			
			if(numFilesQueued>0){
				try{
					/* 开始上传方法 */
					//this.startUpload();
				}
				catch(ex){
					this.debug(ex);
				}
			}
		}
		
		var uploadfilenums = 0;
		
		// 加载默认已上传好的数据
		this.showimages=function(imagevalues){
			if(imagevalues.length<5)return false;
			$('#'+swfu.swfuploadimageshow).html('');
			$('#'+swfu.swfuploadimageslistid).val(imagevalues);
			filevalues = imagevalues.split('{lensplit}');
			$.each(filevalues,function(key,val){
				uploadfilenums++;
				var serverData = val.split('{split}');
				$('#'+swfu.swfuploadimageshow).append('<li id="'+swfu.swfuploadimageslistid+'_swfuploadimages_'+uploadfilenums+'"><img src="'+serverData[0]+'" /><div><a href="javascript:;" _src="'+serverData[0]+'" id="'+swfu.swfuploadimageslistid+'_imagelistdel_'+uploadfilenums+'">移除</a></div><span><input type="text" value="'+serverData[1]+'" placeholder="描述" id="'+swfu.swfuploadimageslistid+'text_'+uploadfilenums+'" /></span></li>');
				var i=uploadfilenums;
				// 删除
				$('#'+swfu.swfuploadimageslistid+'_imagelistdel_'+uploadfilenums).click(function(){
					var tempfilevalues =new Array();
					var delimg=$(this).attr('_src');
					$.each(filevalues,function(key,val){
						var vals=val.split('{split}');
						if(vals[0]!=delimg){
							tempfilevalues.push(val);
						}
					});
					filevalues = tempfilevalues;
					$('#'+swfu.swfuploadimageslistid).val(filevalues.join('{lensplit}'));
					$('#'+swfu.swfuploadimageslistid+'_swfuploadimages_'+i).remove();
					
				});
				
				$('#'+swfu.swfuploadimageslistid+'text_'+uploadfilenums).blur(function(){
					var tempfilevalues =new Array();
					var values = $(this).val();
					$.each(filevalues,function(key,val){
						var vals=val.split('{split}');
						if(vals[0]==serverData[0]){
							tempfilevalues.push(vals[0]+'{split}'+values);
						}else{
							tempfilevalues.push(val);
						}
					});
					filevalues = tempfilevalues;
					$('#'+swfu.swfuploadimageslistid).val(filevalues.join('{lensplit}'));
				});
			});
		}
		
		
		/*
			这个方法作用： 就是上传成功后执行,这里上传成功不是说真的把图片上传成功了，只是把数据提交到服务器成功了。
			参数：(file object, server data)
		*/
		
		function uploadSuccess(file,serverData){
			if(serverData.substring(0, 6)=='ERROR:'){
				var upservermessage=[
				"上传成功", // 0上传成功;
				"文件大小超出了服务器的限制", // 1
				"要上传的文件大小超出浏览器限制", // 2
				"文件仅部分被上传", // 3
				"没有找到要上传的文件", // 4
				"服务器临时文件夹丢失",  // 5
				"文件写入到临时文件夹出错",  // 6
				"上传文件格式不正确",   // 7
				"上传文件太大了", //8
				"文件保存时出错", //9
				];
				alert(upservermessage[serverData.substring(6)]);
			}else{
				uploadfilenums++;
				//if(serverData.substring(0, 7)=='FILEID:'){serverData = serverData.substring(7)}
				//$('#'+swfu.swfuploadimageslistid).val(serverData);
				//alert('文件 "'+file.name+'" 已上传成功！');
				callbackfilenamne(file.name,serverData);
			}
		}
		/*
			作用：主要是进度条
			参数：uploadProgress(file object, bytes complete, total bytes)
			     bytes 上传字节数 ，total 总字节数
		*/
		function uploadProgress(file, bytesLoaded,total ){
			var c=Math.floor((bytesLoaded/total)*100);
			if(c<100){
				var num=bytesLoaded/(1024*1024);
				fileSize=Math.round(num*100)/100+"MB";
				$('#del_'+file.id).outerHTML('<span id="del_'+file.id+'">已上传:'+fileSize+'</span>');
			}else{
				$('#del_'+file.id).outerHTML('<span id="del_'+file.id+'">保存中..</span>');
			}
			$('#uploadprogress_'+file.id+'').css({width:c+'px'});
			$('#uploadprogresssize_'+file.id+'').html('&nbsp;'+c+'%');
		}
		
		/*
			作用：就是循环上传队列的图片
			参数：file Object
		*/
		function uploadComplete(file){
			// 删除队列中已上传的
			$('#file_'+file.id).animate({height: 'toggle', opacity: 'toggle'},500,function(){$(this).remove();});
			//$('#file_'+file.id).hide().remove();
			if(this.getStats().files_queued > 0){
				this.startUpload();
			}else{
				$('#'+swfu.swfuploadbox).animate({height: 'toggle', opacity: 'toggle'},500,function(){
					//alert('上传完成');
				});
				
			}
		}
		
		/*
			作用： 把所选的图片入列。
			参数： file Object.
		*/
		function fileQueued(file){
			var fileSize=Math.floor((file.size/1024)/1024)+"MB";
			var fileSize;
			var num=file.size/(1024*1024);
			fileSize=Math.round(num*100)/100+"MB";
			$("#"+swfu.filelistbox).append('<li id="file_'+file.id+'"><table width="560" border="0" cellspacing="0" cellpadding="0"><tr><td height="24" width="40" style="color:#A8A8A8; text-align:center">名称:</td><td><div>'+file.name+'</div></td><td wdith="40" style="color:#A8A8A8; text-align:center">大小:</td><td width="60">'+fileSize+'</td><td width="250"><span class="swfuploadjingdutiao"><div id="uploadprogress_'+file.id+'"></div></span>&nbsp;<span style="float:left" id="uploadprogresssize_'+file.id+'">&nbsp;0%</span><a href="javascript:;" id="del_'+file.id+'">取消上传</a></td></tr></table></li>');
			$('#'+swfu.swfuploadbox).show();
			/**
				删除上传队列指定的文件
			*/
			$('#del_'+file.id).click(function(){
				swfu.cancelUpload(file.id);
				$('#file_'+file.id).remove();
			});
		}
}
</script>
<style type="text/css">
body{font-family: "microsoft yahei","微软雅黑","宋体", Arial, sans-serif; margin:0; padding:0;}
.swfuplaodimg{ margin:0px; padding:0px; border:red solid 1px;}
.swfuploadimg li{ width:130px; margin:0px; margin-right:10px; float:left; list-style:none;}
.swfuploadimg li img{ width:130px; border:#CCC solid 1px; height:110px;}
.swfuploadimg li span{ display:block; width:130px;}
.swfuploadimg li span input{ width:130px; border:#CCC solid 1px; margin-top:-2px;}

.swfuploadfilebox{ width:560px; display:none}
.swfuploadfilelist{margin:0px; padding:0px; width:560px; }
.swfuploadfilelist li{ list-style:none; font-size:12px; margin-top:-1px; border:#EEEEEE solid 1px;}
.swfuploadfilelist li .swfuploadjingdutiao{border-radius:6px; float:left; display:block; width:100px; background:#EEEEEE; border:#EEEEEE solid 1px; height:12px; overflow:hidden;}
.swfuploadfilelist li .swfuploadjingdutiao div{border-radius:6px; width:0px;  height:12px; background:#0099cc; overflow:hidden;}
</style>
</head>

<body>
<table width="560" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="170" valign="top"><span id="<?php echo $boxid;?>_swfupload_selectid"></span></td>
    <td valign="top"><img id="<?php echo $boxid;?>_swfuploadid"  style="cursor:pointer; float:left; margin-right:10px;" src="../lzsswfupload/newbigstartupload.png" /><span style="float:left; color:#0099cc; font-size:13px; line-height:29px; margin-right:10px;"><?php echo isset($_GET['tips'])?$_GET['tips']:'*.*';?></span></td>
  </tr>
</table>
<div class="swfuploadfilebox" id="<?php echo $boxid;?>_filelist"></div><div style="text-align:right"></div>
<ul class="swfuploadimg" style="padding:0px;" id="<?php echo $boxid;?>_imageslist"></ul>
<script type="text/javascript">
var myswf = new Bigupload('<?php echo $boxid;?>_swfupload_selectid','<?php echo $boxid;?>_swfuploadid','<?php echo $boxid;?>_filelist','<?php echo $boxid;?>_filelistbox','<?php echo $boxid;?>','<?php echo $boxid;?>_imageslist');
myswf.start();
function callbackfilenamne(filename,savename){
	parent.document.getElementById("<?php echo $boxid;?>").value=savename;
    alert('文件 "'+filename+'" 已上传成功！');
}
</script>
</body>
</html>
